Lab 9: Social Network Traversal

The Goal 🎯

  • The Model: A simple social network.
    • Users are represented as nodes in a graph.
    • "Follows" are directed edges.
  • The Task: Process commands to build the network and traverse it using DFS and BFS.

The Representation 💾

We will use an Adjacency List to store the graph.

The list at index `i` stores all the users that user `i` follows.

// Follows: 0->1, 0->2, 1->3
adj = [
0: [1, 2],
1: [3],
2: [],
3: [],
]

The Operations ⚙️

You will implement three commands:

  • add u v

    User `u` follows `v`.

  • dfs u

    Depth-First Search from `u`.

  • bfs u

    Breadth-First Search from `u`.